package com.wang.xh.category.controller;

import com.wang.xh.api.domain.category.Category;
import com.wang.xh.category.service.CategoryService;
import com.wang.xh.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/category")
@Tag(name = "显示分类")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "获得所有的分类")
    @GetMapping(value = "/mutli")
    public R getAllCategory() {
        List<Category> category = categoryService.list();
        return R.ok(category);
    }

    @Operation(summary = "增加分类")
    @PostMapping(value = "/addCategory")
    public R addCategory(@RequestBody Category category) {
        boolean flag = categoryService.save(category);
        if (flag) {
            return R.ok("增加分类成功");
        } else {
            return R.error("增加分类失败");
        }
    }

    @Operation(summary = "删除分类数据")
    @DeleteMapping(value = "/deleteCategory/{id}")
    public R deleteCategory(@PathVariable("id") Integer bannerId) {
        boolean flag = categoryService.removeById(bannerId);
        if (flag) {
            return R.ok("删除分类成功");
        } else {
            return R.error("删除分类失败");
        }
    }

    @Operation(summary = "按照分类编号查询数据")
    @GetMapping(value = "/getCategoryById/{id}")
    public R getByIdCategory(@PathVariable("id") Integer bannerId) {
        Category category = categoryService.getById(bannerId);
        return R.ok(category);
    }

    @Operation(summary = "修改分类数据")
    @PutMapping(value = "/updateCategory")
    public R updateCategory(@RequestBody Category category) {
        boolean flag = categoryService.updateById(category);
        if (flag) {
            return R.ok("修改分类成功");
        } else {
            return R.error("修改分类失败");
        }
    }
}
